//
//  GiftViewController.m
//  PhoenixNews
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "GiftViewController.h"
#import "PhoneixNews.pch"
#import "HomePageCollectionViewCell.h"
#import "HomePageTableViewCell.h"
#import "DanPinViewController.h"
@interface GiftViewController ()<UITableViewDataSource, UITableViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *mutableArray;// 轮播图数组
@property (nonatomic, retain) NSMutableArray *imageArray;// 内容数组
@property (nonatomic, retain) NSMutableArray *LunArray;
@end

@implementation GiftViewController
- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
       
    // tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 140)style: UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[HomePageTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    [self getData];
    [self getHeaderData];
    [self getLunData];
    
   
}

#pragma mark - 获取数据

- (void)getData {
    // 轮播图数据
     AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:HomePage parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        NSArray *array = dic[@"banners"];
        [self.tableView reloadData];
        self.mutableArray = [HeadlineModel modelHanderWithArray:array];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    }
- (void)getHeaderData {
    //  内容数据
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:HomePageImage parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        NSArray *array = dic[@"items"];
        self.imageArray = [HeadlineModel modelHanderWithArray:array];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}
- (void)getLunData {
     AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:LunPage parameters:self progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        NSArray *array = dic[@"secondary_banners"];
        self.LunArray = [HeadlineModel modelHanderWithArray:array];
        [self.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    }

#pragma mark - TableView协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.imageArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    HeadlineModel *homepage = _imageArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:homepage.cover_image_url]];
    cell.title.text = homepage.title;
    cell.imageXin.image = [UIImage imageNamed:@"Xin"];
    ;
    cell.lable.text = [NSString stringWithFormat:@"%@", homepage.likes_count];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 200;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 450;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    headView.backgroundColor = [UIColor whiteColor];
    NSMutableArray *array = [NSMutableArray array];
    UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    
    ScrollWithImage *scroll = [[ScrollWithImage alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    for (int i = 0; i < _mutableArray.count; i++) {
        HeadlineModel *homePage = _mutableArray[i];
        [array addObject:homePage.image_url];
        
        [headView addSubview:scroller];
    }
    
    UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc] init];
    layou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layou.itemSize = CGSizeMake(150, (667 - 64 -3) / 4);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 320, self.view.frame.size.width, 120) collectionViewLayout:layou];
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    [collectionView registerClass:[HomePageCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [headView addSubview:collectionView];
    
    [scroller addSubview:scroll];
    [scroll setArray:array];
    return headView;
    
    
    
}
- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.LunArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HomePageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    HeadlineModel *homePage = self.LunArray[indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:homePage.image_url]];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    DetailePageViewController *urlVC= [[DetailePageViewController alloc] init];
    HeadlineModel *homepage = _imageArray[indexPath.row];
    urlVC.urlStr = homepage.url;
    [self.navigationController pushViewController:urlVC animated:YES];
    
}
#pragma mark - 头


//tableView尾部视图高度为0
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
